/**
 * @Title: HomeActivity.java
 * @Package com.sinoglobal.waitingMe.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-8-30 上午11:55:09
 */

package com.sinoglobal.wallet.activity;
import java.util.ArrayList;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.adapter.W_BalanceDetailAdapter;
import com.sinoglobal.wallet.adapter.W_CouponAdapter;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoValueManager;
import com.sinoglobal.wallet.entity.W_BalanceEntity;
import com.sinoglobal.wallet.entity.W_BalanceEntity.RsEntity;
import com.sinoglobal.wallet.entity.W_CouponListEntity;
import com.sinoglobal.wallet.entity.W_CouponListEntity.CouponList;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.LogUtils;
import com.sinoglobal.wallet.util.NetWorkUtil;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnFooterRefreshListener;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnHeaderRefreshListener;

/**
 * @author clf
 * @createdate 2015-10-22
 * @Description: TODO(余额明细)
 */
public class W_CouponActivity extends W_ShareAbstractActivity implements OnFooterRefreshListener, OnHeaderRefreshListener {

	private PopupWindow menuPop;
	private ArrayList<CouponList> list=new  ArrayList<CouponList>();
	private W_CouponAdapter  adapter;
	private ListView coupon_listview;
	private boolean isShowProgressDialog=true;
	private int page=1;
	private int count=10;
	private int totalpages;//总页数
	private LinearLayout coupon_top_layout;
	private LinearLayout coupon_popu_layout;
	private RelativeLayout view,wallet_nodata_coupon_layout;
	private ImageView coupon_common_img,coupon_enity_img,coupon_vouchers_img,coupon_all_img,coupon_coupons_img;
	private int useState=0;
	private String couponType="";
	private TextView coupon_all_txt,coupon_vouchers_txt,coupon_common_txt,coupon_entity_txt,coupon_coupons_txt;
	private WPullToRefreshView coupon_pulltorefresh;
	private RelativeLayout wallet_coupon_unuse_layout,wallet_coupon_used_layout;
	private TextView wallet_coupon_unuse_txt,wallet_coupon_used_txt,wallet_nodata_coupon_outdate_txt;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_coupon_layout);
		mTemplateTitleText.setText("我的优惠券");
		showView(mTemplateRightText);
		showView(wallet_title_middle_right_img);
		mTemplateRightText.setText("说明");
		mTemplateRightText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				goIntent(W_CouponShuoMingActivity.class);
			}
		});
		initView();
		initPopuWindow();
	     getData();
		showPopupWindow();
	}
	private void initView() {
		// TODO Auto-generated method stub
		adapter=new W_CouponAdapter(this);
		coupon_listview = (ListView) findViewById(R.id.coupon_listview);
		coupon_pulltorefresh = (WPullToRefreshView) findViewById(R.id.coupon_pulltorefresh);
		wallet_nodata_coupon_layout = (RelativeLayout) findViewById(R.id.wallet_nodata_coupon_layout);
		wallet_coupon_unuse_layout = (RelativeLayout) findViewById(R.id.wallet_coupon_unuse_layout);
		wallet_coupon_used_layout = (RelativeLayout) findViewById(R.id.wallet_coupon_used_layout);
		coupon_top_layout = (LinearLayout) findViewById(R.id.coupon_top_layout);
		wallet_coupon_unuse_txt = (TextView) findViewById(R.id.wallet_coupon_unuse_txt);
		wallet_coupon_used_txt = (TextView) findViewById(R.id.wallet_coupon_used_txt);
		wallet_nodata_coupon_outdate_txt = (TextView) findViewById(R.id.wallet_nodata_coupon_outdate_txt);
		wallet_nodata_coupon_outdate_txt.setText(Html.fromHtml("<u>" + "已过期优惠券" + "</u>"));
		wallet_coupon_unuse_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
		coupon_listview.setAdapter(adapter);
		coupon_pulltorefresh.setOnFooterRefreshListener(this);
		coupon_pulltorefresh.setOnHeaderRefreshListener(this);
		wallet_coupon_used_layout.setOnClickListener(this);
		wallet_coupon_unuse_layout.setOnClickListener(this);
		wallet_nodata_coupon_outdate_txt.setOnClickListener(this);
		coupon_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			                 CouponList coupon = list.get(position);
			                 int use_STATE = coupon.getUSE_STATE();
			                 if(use_STATE==0){
                           Bundle bundle = new Bundle();
                           bundle.putString("COUPON_TYPE",coupon.getCOUPON_TYPE());
                           bundle.putInt("COUPON_CLAIM_ID",coupon.getCOUPON_CLAIM_ID());
                           bundle.putString("LOGO_IMAGE",coupon.getLOGO_IMAGE());
                           bundle.putString("COUPON_NAME",coupon.getCOUPON_NAME());
                           bundle.putString("THIRD_PARTY_NAME",coupon.getTHIRD_PARTY_NAME());
                           bundle.putInt("USE_STATE",coupon.getUSE_STATE());
                           goIntent(W_CouponDetailActivity.class,bundle);}
			}
		});
	}
	private void initPopuWindow(){
		view = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.wallet_coupon_popu_list_layout, null);
		coupon_popu_layout = (LinearLayout) view.findViewById(R.id.coupon_popu_layout);
		coupon_common_img = (ImageView) view.findViewById(R.id.coupon_common_img);
		coupon_enity_img = (ImageView) view.findViewById(R.id.coupon_enity_img);
		coupon_vouchers_img = (ImageView) view.findViewById(R.id.coupon_vouchers_img);
		coupon_all_img = (ImageView) view.findViewById(R.id.coupon_all_img);
		coupon_coupons_img = (ImageView) view.findViewById(R.id.coupon_coupons_img);
		coupon_all_txt= (TextView) view.findViewById(R.id.coupon_all_txt);
		coupon_common_txt = (TextView) view.findViewById(R.id.coupon_common_txt);
		coupon_entity_txt = (TextView) view.findViewById(R.id.coupon_entity_txt);
		coupon_vouchers_txt = (TextView) view.findViewById(R.id.coupon_vouchers_txt);
		coupon_coupons_txt = (TextView) view.findViewById(R.id.coupon_coupons_txt);
		coupon_coupons_txt.setOnClickListener(this);
		coupon_all_txt.setOnClickListener(this);
		coupon_common_txt.setOnClickListener(this);
		coupon_entity_txt.setOnClickListener(this);
		coupon_vouchers_txt.setOnClickListener(this);
	}
	private void showPopupWindow() {
		if (menuPop == null) {
			menuPop = new PopupWindow(this);
			menuPop.setWidth(LayoutParams.MATCH_PARENT);
			menuPop.setHeight(LayoutParams.MATCH_PARENT);
			menuPop.setBackgroundDrawable(new BitmapDrawable());
			menuPop.setFocusable(true);
			menuPop.setOutsideTouchable(true);
		}
		menuPop.setContentView(view);
		 view.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View v) {

	            	popuDismiss();
	            }
	        });
		  }

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id==R.id.wallet_title_middle_tv){
			coupon_popu_layout.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.w_push_top_in));
			menuPop.showAsDropDown(mTemplateLayout);
			menuPop.update();
		}else if(id==R.id.wallet_coupon_unuse_layout){//未使用优惠券
			wallet_coupon_unuse_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));	
			wallet_coupon_used_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));	
			page=1;
			useState=0;
			list.clear();
			getData();
		}else if(id==R.id.wallet_coupon_used_layout){//已使优惠券
			wallet_coupon_used_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
			wallet_coupon_unuse_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));	
			page=1;
			useState=1;
			list.clear();
			getData();
			//getSpendingList();
		}else if(id==R.id.coupon_all_txt){
			coupon_all_img.setVisibility(View.VISIBLE);
			coupon_common_img.setVisibility(View.GONE);
			coupon_enity_img.setVisibility(View.GONE);
			coupon_vouchers_img.setVisibility(View.GONE);
			coupon_coupons_img.setVisibility(View.GONE);
			  setTextColor(id);//更改选择字体的颜色
		       couponType="";
				page=1;
				list.clear();
				getData();
		        popuDismiss();
		}else if(id==R.id.coupon_common_txt){
			coupon_common_img.setVisibility(View.VISIBLE);
			coupon_enity_img.setVisibility(View.GONE);
			coupon_vouchers_img.setVisibility(View.GONE);
			coupon_all_img.setVisibility(View.GONE);
			coupon_coupons_img.setVisibility(View.GONE);
			   setTextColor(id);
			   couponType="A";
			   page=1;
				list.clear();
				getData();
		        popuDismiss();
		}else if(id==R.id.coupon_vouchers_txt){
			coupon_all_img.setVisibility(View.GONE);
			coupon_common_img.setVisibility(View.GONE);
			coupon_enity_img.setVisibility(View.GONE);
			coupon_vouchers_img.setVisibility(View.VISIBLE);
			coupon_coupons_img.setVisibility(View.GONE);
			  setTextColor(id);
			  couponType="B";
				page=1;
				list.clear();
				getData();
		        popuDismiss();
		}else if(id==R.id.coupon_coupons_txt){
			coupon_all_img.setVisibility(View.GONE);
			coupon_common_img.setVisibility(View.GONE);
			coupon_enity_img.setVisibility(View.GONE);
			coupon_coupons_img.setVisibility(View.VISIBLE);
			coupon_vouchers_img.setVisibility(View.GONE);
			  setTextColor(id);
			  couponType="C";
				page=1;
				list.clear();
				getData();
		        popuDismiss();
		}else if(id==R.id.coupon_entity_txt){
			coupon_all_img.setVisibility(View.GONE);
			coupon_common_img.setVisibility(View.GONE);
			coupon_enity_img.setVisibility(View.VISIBLE);
			coupon_vouchers_img.setVisibility(View.GONE);
			coupon_coupons_img.setVisibility(View.GONE);
			   setTextColor(id);
			   couponType="D";
				page=1;
				list.clear();
				getData();
		        popuDismiss();
		}else if(id==R.id.wallet_nodata_coupon_outdate_txt){
			goIntent(W_OutDateCouponActivity.class);
		}else if(id==R.id.wallet_noData){
			 if(NetWorkUtil.getNetWorkInfoType(getApplicationContext())==NetWorkUtil.NONE){
					showToast("请检查网络是否连接！");	
					 loadError(true);
					}else{
						loadError(false);;
						getData();
					}
	}
	}
	private void setTextColor(int id) {
		// TODO Auto-generated method stub
		if(id==R.id.coupon_all_txt){
			coupon_all_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
			coupon_common_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_entity_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_vouchers_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_coupons_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
		}else if(id==R.id.coupon_common_txt){
			coupon_all_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_common_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
			coupon_entity_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_vouchers_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_coupons_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
		}else if(id==R.id.coupon_vouchers_txt){
			coupon_all_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_common_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_entity_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_vouchers_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
			coupon_coupons_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
		}else if(id==R.id.coupon_coupons_txt){
			coupon_all_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_common_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_entity_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_vouchers_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_coupons_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
		}else if(id==R.id.coupon_entity_txt){
			coupon_all_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_common_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_entity_txt.setTextColor(getResources().getColor(R.color.w_coupon_top_txtcolor));
			coupon_vouchers_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
			coupon_coupons_txt.setTextColor(getResources().getColor(R.color.w_orderPay_black));
		}
	}
	@Override
	public void onHeaderRefresh(WPullToRefreshView view) {
		// TODO Auto-generated method stub
		coupon_pulltorefresh.setEnablePullLoadMoreDataStatus(true);
		isShowProgressDialog=false;
		page=1;
		getData();
	}
	@Override
	public void onFooterRefresh(WPullToRefreshView view) {
		// TODO Auto-generated method stub
		isShowProgressDialog=false;
		if (page <=totalpages) {
			getData();
		
		} else {
			Toast.makeText(W_CouponActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
			coupon_pulltorefresh.setEnablePullLoadMoreDataStatus(false);
			coupon_pulltorefresh.onFooterRefreshComplete();
		}
	}
	private void popuDismiss() {
		Animation Animation = AnimationUtils.loadAnimation(this,R.anim.w_push_top_out);
		coupon_popu_layout.startAnimation(Animation);
		Animation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				 // LogUtils.v(  "mpopupWindow.dismiss();---------");
				menuPop.dismiss();
			}
		});
	}
	  private void getData() {
			MyAsyncTask<W_CouponListEntity> task = new MyAsyncTask<W_CouponListEntity>(isShowProgressDialog,this,true) {
				@Override
				public W_CouponListEntity before(Void... params) throws Exception {
					// TODO 测试接口
					return RemoteImpl.getInstance().getCouponList(W_CouponActivity.this, page,useState, count,couponType);
				}
				@Override
				public void after(W_CouponListEntity result) {
					if (result != null) {
						if (result.getCode().equals("0000")) {
							
							  loadError(false);
								if(result.getCouponList().size()==0){
								
									wallet_nodata_coupon_layout.setVisibility(View.VISIBLE);
									return;
								} 
								wallet_nodata_coupon_layout.setVisibility(View.GONE);
								if (page == 1) {
									list=(ArrayList<CouponList>) result.getCouponList();
				                       adapter.setListData(list);
				                      
				                    } else {
				                    	list.addAll(result.getCouponList());
				                    }
							
										adapter.setListData(list);
									
									
										 totalpages = result.getTotalPage();
									
									page++;
									coupon_pulltorefresh.onFooterRefreshComplete();
									coupon_pulltorefresh.onHeaderRefreshComplete();
						}else{
							coupon_pulltorefresh.onFooterRefreshComplete();
							coupon_pulltorefresh.onHeaderRefreshComplete();
							Toast.makeText(W_CouponActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}else{
						coupon_pulltorefresh.onFooterRefreshComplete();
						coupon_pulltorefresh.onHeaderRefreshComplete();
						Toast.makeText(W_CouponActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
					}
					
				}

				@Override
				public void exception() {
				}
			};
			task.execute();
		}

}
